package com.doubleview.demo
/**
 * @author huchengchao <huchengchao@kuaishou.com>
 * Created on 2021-11-20
 */

//====================基础操作====================
def map = [name: 'Gromit', likes: 'cheese', id: 1234]
assert map.name == 'Gromit'     // 同map.get('Gromit')
assert map.id == 1234

def emptyMap = [:]
assert emptyMap.size() == 0
emptyMap.foo = 5
assert emptyMap.size() == 1
assert emptyMap.foo == 5


map = [name: 'Gromit', likes: 'cheese', id: 1234]
assert map.class == null
assert map.get('class') == null
assert map.getClass() == LinkedHashMap // 这很可能是你想要的结果

map = [1      : 'a',
       (true) : 'p',
       (false): 'q',
       (null) : 'x',
       'null' : 'z']
assert map.containsKey(1) // 数字1不是一个标识符，所以得这样调用
assert map.true == null
assert map.false == null
assert map.get(true) == 'p'
assert map.get(false) == 'q'
assert map.null == 'z'
assert map.get(null) == 'x'

//=================迭代======================
map = [
        Bob  : 42,
        Alice: 54,
        Max  : 33
]

// `entry` is a map entry
map.each { entry ->
    println "Name: $entry.key Age: $entry.value"
}

// `entry` is a map entry, `i` the index in the map
map.eachWithIndex { entry, i ->
    println "$i - Name: $entry.key Age: $entry.value"
}

// Alternatively you can use key and value directly
map.each { key, value ->
    println "Name: $key Age: $value"
}

// Key, value and i as the index in the map
map.eachWithIndex { key, value, i ->
    println "$i - Name: $key Age: $value"
}

//=================添加或删除元素======================
def defaults = [1: 'a', 2: 'b', 3: 'c', 4: 'd']
def overrides = [2: 'z', 5: 'x', 13: 'x']
defaults << [a: "a"]
defaults << [100: "100", 200: "200"]

def result = new LinkedHashMap(defaults)
result.put(15, 't')
result[17] = 'u'
result += overrides
assert result == [1: 'a', 2: 'z', 3: 'c', 4: 'd', 5: 'x', 13: 'x', 15: 't', 17: 'u']
def m = [1: 'a', 2: 'b']
//删除Map的所有元素可以通过调用clear方法来完成
assert m.get(1) == 'a'
m.clear()
assert m == [:]

//不应该使用GString作为Map的键，因为GString的哈希码与等效String的哈希码不同
def key = 'some key'
map = [:]
def gstringKey = "${key.toUpperCase()}"
map.put(gstringKey, 'value')
assert map.get('SOME KEY') == null


//=================过滤和搜索======================
def people = [
        1: [name: 'Bob', age: 32, gender: 'M'],
        2: [name: 'Johnny', age: 36, gender: 'M'],
        3: [name: 'Claire', age: 21, gender: 'F'],
        4: [name: 'Amy', age: 54, gender: 'F']
]

def bob = people.find { it.value.name == 'Bob' } // 查找单个entry
def females = people.findAll { it.value.gender == 'F' }

//两个都是返回entries，但是您可以使用collect来检索年龄例如
def ageOfBob = bob.value.age
def agesOfFemales = females.collect {
    it.value.age
}

assert ageOfBob == 32
assert agesOfFemales == [21, 54]

// 但您也可以使用键/对值作为闭包的参数but you could also use a key/pair value as the parameters of the closures
def agesOfMales = people.findAll { id, person ->
    person.gender == 'M'
}.collect { id, person ->
    person.age
}
assert agesOfMales == [32, 36]

// `every` 如果所有entries都匹配规则，则返回true
assert people.every { id, person ->
    person.age > 18
}

// `any` 如果任何一个entry匹配规则，则返回true
assert people.any { id, person ->
    person.age == 54
}


//======================分组=======================
assert ['a', 7, 'b', [2, 3]].groupBy {
    it.class
} == [(String)   : ['a', 'b'],
      (Integer)  : [7],
      (ArrayList): [[2, 3]]
]

assert [
        [name: 'Clark', city: 'London'], [name: 'Sharma', city: 'London'],
        [name: 'Maradona', city: 'LA'], [name: 'Zhang', city: 'HK'],
        [name: 'Ali', city: 'HK'], [name: 'Liu', city: 'HK'],
].groupBy { it.city } == [
        London: [[name: 'Clark', city: 'London'],
                 [name: 'Sharma', city: 'London']],
        LA    : [[name: 'Maradona', city: 'LA']],
        HK    : [[name: 'Zhang', city: 'HK'],
                 [name: 'Ali', city: 'HK'],
                 [name: 'Liu', city: 'HK']],
]
